package com.therandoms.rtgdroid;

import java.io.IOException;
import java.io.InputStream;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.sun.jersey.spi.service.ServiceFinder;
import com.therandoms.rtgdroid.background.FileCopier;
import com.therandoms.rtgdroid.stupidhacks.AndroidServiceIteratorProvider;

import de.keyboardsurfer.android.widget.crouton.Crouton;

/**
 * A simple main activity 
 * @author Vinh Pham
 * @author Logan Widick
 */
public class MainActivity extends FragmentActivity {

	/**
	 * The default directory to put the grammar files in
	 */
	private static final String GRAMMAR_DIR = "grammars";
	
	/**
	 * The name of the default grammar file
	 */
	private static final String GRAMMAR_FILE = "grammar.xml";
	
	/**
	 * The log tag for the activity (debugging)
	 */
	@SuppressWarnings("unused")
	private static final String TAG = "MainActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//show the activity
		setContentView(R.layout.activity_main);
		
		//stupid hack stuff
		ServiceFinder.setIteratorProvider(new AndroidServiceIteratorProvider<Object>());
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.action_settings) {
			startActivity(new Intent(MainActivity.this, SettingsActivity.class));
		}
		else if(item.getItemId() == R.id.action_about){
			startActivity(new Intent(MainActivity.this, AboutActivity.class));
		}
		else if(item.getItemId() == R.id.action_help){
			startActivity(new Intent(MainActivity.this, HelpActivity.class));
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();

		try {
			InputStream in = getAssets().open("grammar.xml");
			FileCopier.copyResourceToFile(in, GRAMMAR_DIR,
					GRAMMAR_FILE);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onDestroy() {
		Crouton.cancelAllCroutons();
		super.onDestroy();
	}
}